package net.newsmth.smthapp.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import net.newsmth.smthapp.android.R;
import net.newsmth.smthapp.android.helper.SectionHelper;
import net.newsmth.smthapp.android.model.BBSThread;
import net.newsmth.smthapp.android.model.Section;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Hatsune Miku on 4/11/14.
 */
public class HotFragmentAdapter extends BaseExpandableListAdapter {
    private Context context;
    private SectionHelper sectionHelper;
    private LayoutInflater inflater;

    private Map<Integer, List<BBSThread>> buffer = new TreeMap<Integer, List<BBSThread>>();

    public HotFragmentAdapter(Context context, SectionHelper sectionHelper) {
        this.context = context;
        this.sectionHelper = sectionHelper;

        inflater = LayoutInflater.from(this.context);
    }


    @Override
    public int getGroupCount() {
        return sectionHelper.getAllSections().size();
    }

    @Override
    public int getChildrenCount(int i) {
        return sectionHelper.getThreadsFromSection(i).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        List<Section> sections = sectionHelper.getAllSections();
        return sections.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (buffer.containsKey(groupPosition)) {
            return buffer.get(groupPosition).get(childPosition);
        } else {
            Section section = (Section)getGroup(groupPosition);
            List<BBSThread> threads = sectionHelper.getThreadsFromSection(section.getId());

            buffer.put(groupPosition, threads);
            return threads.get(childPosition);
        }
    }

    @Override
    public long getGroupId(int groupPosition) {
        Section section = (Section)getGroup(groupPosition);

        return section.getId();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        BBSThread thread = (BBSThread)getChild(groupPosition, childPosition);
        return thread.getId();
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Section section = (Section)getGroup(groupPosition);

        TextView groupView = getGroupView(convertView, parent);
        groupView.setText(section.getTitle());


        return groupView;
    }

    private TextView getGroupView(View convertView, ViewGroup parent) {
        if (convertView == null) {
            return (TextView)inflater.inflate(R.layout.view_hot_group, null);
        } else {
            return (TextView)convertView;
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        BBSThread thread = (BBSThread)getChild(groupPosition, childPosition);

        View childView = getChildView(convertView);
        TextView titleTextView = (TextView)childView.findViewById(R.id.thread_subject_text_view);
        TextView sectionTextView = (TextView)childView.findViewById(R.id.thread_section_text_view);


        titleTextView.setText(thread.getSubject());
        sectionTextView.setText(thread.getBoard());

        return childView;
    }

    private View getChildView(View convertView) {
        if (convertView == null) {
            return inflater.inflate(R.layout.view_hot_child, null);
        } else {
            return convertView;
        }
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return (groupId + 1);
    }

    public void refresh() {
        this.buffer.clear();
    }
}
